package com.browsesoft.log;

import java.util.Hashtable;

import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.Tools;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.user.User;

/**
 * 日值helper
 * 
 */
public class LogHelper {

	/**
	 * 实体插入和更新操作日志,不支持通过delete.jsp得删除操作
	 * 
	 * @param operate
	 *            操作表示，必须和页面中的表示想对应 eg "insert" 页面中
	 * @param component
	 *            组件
	 * @param attrs
	 *            属性名值集合
	 */
	public synchronized static void logEntity(String operate, User user,
			HTMLBasicComponent component, Hashtable attrs) {
		// 得到页面中的log描述元素
		Element logElement = getLogDescElement(component);
		// 得到日值类型，描述信息
		String type = logElement.getAttribute("type");
		String operateDesc = getOperateDesc(logElement, operate, attrs);
		// 记录日志
		OperateLog log = new OperateLog();
		log.logEntityOperate(user, type, operateDesc);
	}

	/**
	 * 得到页面中的日值描述元素
	 */
	private static Element getLogDescElement(HTMLBasicComponent comp) {
		Element result = (Element) comp.getPage().getElement()
				.getElementsByTagName("log").item(0);
		return result;
	}

	/**
	 * 得的操作描述
	 * 
	 * @param logElem
	 * @param operate
	 * @param attrs
	 * @return
	 */
	private static String getOperateDesc(Element logElem, String operate,
			Hashtable attrs) {
		String result = "";
		// 根据动作标识 description ,替换属性
		Element descElem = (Element) logElem.getElementsByTagName(operate)
				.item(0);
		result = descElem.getAttribute("description");
		result = Tools.replaceAttrs(result, attrs);
		// 如果有其他#xxx#属性
		while (result.indexOf("#") != -1) {
			String temp = result.substring(result.indexOf("#") + 1);
			int end = temp.indexOf("#");
			String exp = temp.substring(0, end);
			// 根据属性名找到配置元素，得到属性
			String value = getExpAttrValue(descElem, exp, attrs);
			result = result.replaceAll("#" + exp + "#", value);
		}
		return result;
	}

	/**
	 * 根据配置元素得到属性
	 */
	private static String getExpAttrValue(Element descElem, String exp,
			Hashtable attrs) {
		try {
			Element elem = (Element) descElem.getElementsByTagName(exp).item(0);
			// 根据配置的findkey从该记录属性中得到属性值
			String findKey = elem.getAttribute("findkey");
			String keyValue = (String) attrs.get(findKey);
			// 如果是sql查找
			if (elem.getAttribute("sqlfind") != null
					&& !elem.getAttribute("sqlfind").equals("")) {
				return getSqlResult(elem, findKey, keyValue);
			}
			// 否则，根据keyValue查找实体对象
			else {
				// 得到父属性描述
				String attributeName = elem.getAttribute("attributename");
				Entity entity = (Entity) EntityManager.getInstance().getObject(
						keyValue);
				return (String) entity.getAttributes().get(attributeName);
			}
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 如果是sql查找
	 */
	private static String getSqlResult(Element elem, String findKey,
			String keyValue) throws Exception {
		String sql = elem.getAttribute("sqlfind");
		sql = sql.replaceAll("#" + findKey + "#", keyValue);
		String[][] datas = DBTools.executeQueryWithTableHead(sql);
		return datas[1][0];
	}

	/**
	 * 记录日志
	 * 
	 * @param operate
	 */
	public synchronized static void logEntity(User user, String type,
			String operate) {
		OperateLog operateLog = new OperateLog();
		operateLog.logEntityOperate(user, type, operate);
	}
}
